//
//  YFTrainingApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2018/1/15.
//  Copyright © 2018年 Coollang-YF. All rights reserved.
//

#import "YFTrainingApi.h"
#import "RealTimeTrainModel.h"
#import "ChartItem.h"

@implementation YFTrainingApi

- (NSInteger)cacheTimeInSeconds
{
    if (self.apiType == KTrainRealTimeGetSearveHistoryAPi ||
        self.apiType == KTrainRealTimeGetForeBackHandHistoryAPi) {
        return 5.0f;
    }else if (self.apiType == kTrainGetHDTennisCoachListApi || self.apiType == kTrainGetOldTennisVideolistAPi){
        return 60.0f * 5;
    } else {
        return [super cacheTimeInSeconds];
    }
}


/**
 * 获取教练视频
 * isHDVideo: Yes 为获取海德的教学视频，NO 为获取老版本的教学视频
 */
+ (instancetype)getTrainTennisCoachVideoWithisHDVideo:(BOOL)isHDVideo {
    RequestApiType apiType = isHDVideo ? kTrainGetHDTennisCoachListApi:kTrainGetOldTennisVideolistAPi;
    if ([NSBundle currentLanguageIsChinese]) {
        return [YFTrainingApi requestApiWithType:apiType andParam:@{@"lang":@"zh_cn"}];
    }else {
        return [YFTrainingApi requestApiWithType:apiType andParam:@{@"lang":@"english"}];
    }
}
/**
 * 获取实时训练历史数据
 * realTimeTrainType: 获取的历史数据类型
 */
+ (instancetype)getTrainRealTimeHistoryData:(RealTimeTrainType)realTimeTrainType withRealTimeDateString:(NSString *)realTimeDateString complete:(void(^)(NSArray *realTimeDatas,NSError *error))completeBlock {
    
    RequestApiType apiType = realTimeTrainType == RealTimeTrainFQ ? KTrainRealTimeGetSearveHistoryAPi:KTrainRealTimeGetForeBackHandHistoryAPi;
    
    YFTrainingApi *getDataApi = [YFTrainingApi requestApiWithType:apiType andParam:@{@"time": realTimeDateString}];
    [[YFNetworkingService sharedInstance] sendRequest:getDataApi callback:^(BOOL success, id result, NSError *error) {
        if (error == nil) {
            // 解析json 数据
            NSArray *array = [result responseInfo];
            // 字典转模型
            NSMutableArray *arrayM = [NSMutableArray array];
            NSLog(@"%@ --- %zd", arrayM, arrayM.count);
            [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                RealTimeTrainModel *model = [[RealTimeTrainModel alloc] initWithGetNetLoadDataDict:obj trainType:realTimeTrainType];
                NSLog(@"%@", model);
                [arrayM addObject:model];
            }];
            if (completeBlock) {
                completeBlock(arrayM.copy,error);
            }
        }else {
            if (completeBlock) {
                completeBlock(nil,error);
            }
        }
    }];
    
    return getDataApi;
}

/**
 * 上传实时训练历史数据
 * realTimeTrainType: 上传的历史数据类型
 */
+ (instancetype)uploadTrainRealTimeRecordDataWithTrainDataModel:(RealTimeTrainModel *)realTimetrainModel {
    NSDictionary *dict;
    if (realTimetrainModel.trainTyep == RealTimeTrainHB) {
        dict = @{
                 @"Date":realTimetrainModel.yearAndMonthAndDay,
                 @"StartTime":@(realTimetrainModel.startRealTimeStamp).stringValue,
                 @"EndTime":@(realTimetrainModel.endRealTimeStamp).stringValue,
                 @"ForceTargetTimes":realTimetrainModel.frontHandPaiSuTargetSpeed.stringValue,
                 @"BackTargetTimes":realTimetrainModel.backHandPaiSuTargetSpeed.stringValue,
                 @"Frequency":[NSString stringWithFormat:@"%.1f",realTimetrainModel.Frequency],
                 @"Detail":[self detailHBdataArrayWithTrainModel:realTimetrainModel],
                 };
    }else {
        dict = @{@"Date":realTimetrainModel.yearAndMonthAndDay,
                 @"StartTime":@(realTimetrainModel.startRealTimeStamp).stringValue,
                 @"EndTime":@(realTimetrainModel.endRealTimeStamp).stringValue,
                 @"TargetSpeed":@(realTimetrainModel.faQiuTargetSpeed).stringValue,
                 @"Detail":[self detailFQdataArrayWithTrainModel:realTimetrainModel],
                 };
    }
    RequestApiType apiType = realTimetrainModel.trainTyep == RealTimeTrainFQ ? KTrainRealTimeSendSearveAPi:KTrainRealTimeSendForeBackHandAPi;
    
    return [YFTrainingApi requestApiWithType:apiType andParam:dict];
    
}

+ (NSString *)detailHBdataArrayWithTrainModel:(RealTimeTrainModel *)realTimetrainModel {
    /*  'T':'1', //动作类型
     'F':'1', //正反手类型
     'S':'30', //速度
     'D':'1234567890'//时间戳*/
    NSMutableArray *arrM = [NSMutableArray array];
    
    [realTimetrainModel.chartItemArray enumerateObjectsUsingBlock:^(ChartItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSDictionary *dict = @{@"T":[@(obj.poseType) stringValue],
                               @"F":[@(obj.handDirectionType) stringValue],
                               @"S":obj.maxSpeed,
                               @"D":obj.unixTimeStamp};
        [arrM addObject:dict];
    }];
    
    NSString *jsonStr = [NSString dictionaryToJson:arrM];
    
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    
    //    NSLog(@"%@", arrM);
    
    return jsonStr.copy;
}
+ (NSString *)detailFQdataArrayWithTrainModel:(RealTimeTrainModel *)realTimetrainModel {
    
    /* 'T':'1', //动作类型
     'S':'30', //速度
     'D':'1234567890'//时间戳*/
    NSMutableArray *arrM = [NSMutableArray array];
    
    [realTimetrainModel.chartItemArray enumerateObjectsUsingBlock:^(ChartItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = @{@"T":@(obj.poseType),
                               @"S":obj.maxSpeed,
                               @"D":obj.unixTimeStamp};
        NSString *jsonStr = [NSString dictionaryToJson:dict];
        
        [arrM addObject:jsonStr];
    }];
    
    NSString *jsonStr = [NSString dictionaryToJson:arrM];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    
    return jsonStr.copy;
}


/**
 * 删除实时训练数据
 * realTimeTrainType: 删除的实时历史数据类型
 */
+ (instancetype)deleteTrainRealTimeHistortDataWithTrainType:(RealTimeTrainType)trainType trainID:(NSString *)ID {
    /*
     参数名    必选    类型   说明
     type    是    string   删除普通训练或者发球训练历史,0表示普通训练,1表示发球训练
     ID    是    string     训练的ID,(每次上传都会返回ID和时间戳,获取历史也会返回ID)
     */
    NSString *type = (trainType == RealTimeTrainHB ? @"0":@"1");
    return [YFTrainingApi requestApiWithType:KTrainRealTimeDeleteHistortAPi andParam:@{@"type":type,@"ID":ID}];
}


@end
